package com.coconet.gas.biz;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.coconet.gas.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

/**
 * Created by Mr.AG Date: 17/1/13 Time: 15:13 Version 1.0.0
 */
public abstract class BaseBiz<M extends Mapper<T>, T> {
	@Autowired
	protected M mapper;

	public void setMapper(M mapper) {
		this.mapper = mapper;
	}

	public T selectOne(T entity) {
		return mapper.selectOne(entity);
	}

	public T selectById(Object id) {
		return mapper.selectByPrimaryKey(id);
	}

	public List<T> selectList(T entity) {
		return mapper.select(entity);
	}

	public List<T> selectListAll() {
		return mapper.selectAll();
	}

	public Long selectCount(T entity) {
		return new Long(mapper.selectCount(entity));
	}

	public void insert(T entity) {
		// EntityUtils.setCreatAndUpdatInfo(entity);
		mapper.insert(entity);
	}

	public int insertSelective(T entity) {
		return mapper.insertSelective(entity);
	}

	public void delete(T entity) {
		mapper.delete(entity);
	}

	public int deleteById(Object id) {
		return mapper.deleteByPrimaryKey(id);
	}

	// public void updateById(T entity) {
	// EntityUtils.setUpdatedInfo(entity);
	// mapper.updateByPrimaryKey(entity);
	// }
	//
	//
	public int updateSelectiveById(T entity) {
		return mapper.updateByPrimaryKeySelective(entity);

	}

	public String fy(T entity, HttpServletRequest request, Integer currentPage, Integer pageSize, Date startime,
			Date endtime, Map<String, Object> map, Map<String, List<Object>> inMap, Map<String, Object> orMap,Map<String, Object> eqMap,
			String sort) {
		PageHelper.startPage(currentPage, pageSize);
		// 根据条件查询 模糊搜索 或者时间限定
		Example example = new Example(entity.getClass());
		if (!StringUtils.isEmpty(sort)) {
			example.setOrderByClause(sort);
		}
	
		Example.Criteria criteria1 = example.createCriteria();
		if (orMap != null) {
			orMap.forEach((key, value) -> {
				criteria1.orEqualTo(key, value);	
			});
		
		}

		Example.Criteria criteria = example.createCriteria();
		if (eqMap!=null) {
			eqMap.forEach((key, value) -> {
				criteria.andEqualTo(key, value);		
			});
		}
		if (map != null) {
			map.forEach((key, value) -> {
				criteria.andLike(key, "%" + value + "%");
			});// 把需要的所有模糊搜索全部加上
		}
		if (entity.toString().contains("Log")) {
			if (!StringUtils.isEmpty(startime) && !StringUtils.isEmpty(endtime)) {
				criteria.andBetween("createTime", startime, endtime);

			}
		}else {
			if (!StringUtils.isEmpty(startime) && !StringUtils.isEmpty(endtime)) {
			criteria.andBetween("createtime", startime, endtime);

		}}
		
		if (inMap != null) {
			inMap.forEach((key, value) -> {
				Example.Criteria criteria2 = example.createCriteria();
				if (value.contains(null)) {				
					criteria2.orIsNull(key);
				} // in null 是搜不出来的 必须 用 isnull
				value.remove(null);
				if (!(value.size() == 0)) {
					criteria2.orIn(key, value);
					
				}
				
				example.and(criteria2);
			});// 把

		}
		//example.or(criteria1);
		
		example.and(criteria);
		
		List<T> users = mapper.selectByExample(example);
		PageInfo<T> pageInfo = new PageInfo<T>(users);

		return JSONObject.toJSONString(pageInfo);

	}

}
